/* Copyright (C) 2021 Rain */

/* This file is part of XNIX. */

/* 
  XNIX is free software: you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation, either version 3 of the License, or 
  (at your option) and later version. 
*/

/*
  XNIX is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  GNU General Public License for more details. 
*/

/*
  You should have received a copy of the GNU General Public License 
   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
*/




#include <kernel/init.h>
#include <kernel/errno.h>
#include <kernel/types.h>

#include <kernel/console.h>
#include <kernel/panic.h>

#include <arch/x86/int.h>
#include <arch/io.h>
#include <arch/sys.h>



static int syslog_index = 0;


void panic(const char *s) {
	printk(red, "kernel panic: %s\n", s);

	trace_syslog();

	for (;;) hlt();
}

void syslog(const char *s) {
	/* we can't use schk in this function, */
	/* because this will make a die-loop (schk_init calls this function) */
	int i;

	for (i = 0; s[i]; i++) {
		if (syslog_index == 32768) 
			syslog_index = 0;
	
		SYSLOG_BUFFER[syslog_index++] = s[i];			
	}


	return;
}


void trace_syslog(void) {
	int i;

	printk(yellow, "tracing system log: \n");

	for (i = 0; i < TRACE_SYSLOG_MAX; i++) 
		putc(SYSLOG_BUFFER[syslog_index + i], yellow);

	return;
}
